<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <div id="123"></div>
    <script src="d3.v4.min.js"></script>
    <script>

const svg = d3.select('body')
.append('svg')
.attr('width',960)
.attr('height',500)

var parseTime = d3.timeParse("%Y");


var margin = {top: 30, right: 50, bottom: 30, left: 30},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    labelPadding = 3;

var g = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("./data/9251.csv", function(error, data) {
  if (error) throw error;

// console.log(data.columns.sclice(1));
console.log(data);

  var series = data.columns.slice(1).map(function(key) {
    return data.map(function(d) {
      return {
        key: key,
        date: d.date,
        value: d[key]
      };
    });
  });

console.log(series);
  var x = d3.scaleTime()
      .domain([data[0].date, data[data.length - 1].date])
      .range([0, width]);

  var y = d3.scaleLinear()
      .domain([0, d3.max(series, function(s) { return d3.max(s, function(d) { return parseInt(d.value); }); })])
      .range([height, 0]);



  var z = d3.schemeCategory10;

  g.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  var serie = g.selectAll(".serie")
      .data(series)
    .enter().append("g")
      .attr("class", "serie");



  serie.append("path")
      .attr("class", "line")
      .style("stroke", function(d,i) { return z[i]; })
      .attr("d", d3.line()
          .x(function(d) { return x(d.date); })
          .y(function(d) { return y(d.value); }));

  var label = serie.selectAll(".label")
      .data(function(d) { return d; })
    .enter().append("g")
      .attr("class", "label")
      .attr("transform", function(d, i) { return "translate(" + x(d.date) + "," + y(d.value) + ")"; });

  label.append("text")
    //   .attr("dy", ".35em")
      .text(function(d) { return d.value; })
    .filter(function(d, i) { return i === data.length - 1; })
    .append("tspan")
      .attr("class", "label-key")
      .text(function(d) { return " " + d.key; });

//   label.append("rect", "text")
//       .datum(function() { return this.nextSibling.getBBox(); })
//       .attr("x", function(d) { return d.x - labelPadding; })
//       .attr("y", function(d) { return d.y - labelPadding; })
//       .attr("width", function(d) { return d.width + 2 * labelPadding; })
//       .attr("height", function(d) { return d.height + 2 * labelPadding; });
});
    </script>
</body>

<style>
    text {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.line {
  fill: none;
  stroke-width: 1.5px;
}

.label {
  text-anchor: middle;
}

.label rect {
  fill: white;
}

.label-key {
  font-weight: bold;
}
</style>
</html>